// pages/catelist/catelist.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    catelist: {
      all: {},
      categories: {},
      sub: [],
      categorizedSub: []
    }
  },


  onLoad(options) {
    this.catelist()
  },

  async catelist() {
    let hotplaylist = await request('/playlist/hot');
    console.log(hotplaylist)
    // let catelist = await request('/playlist/catlist');
    // this.setData({
    //   catelist: {
    //     all: catelist.all,
    //     categories: catelist.categories,
    //     sub: catelist.sub
    //   }
    // })
    // this.classifySubItems();
  },
  classifySubItems: function () {
    const { categories, sub } = this.data.catelist;
    const categorizedSub = [];
    Object.keys(categories).forEach(typeKey => {
      const type = parseInt(typeKey);
      const categoryName = categories[typeKey];
      const items = sub.filter(item => item.type === type);

      if (items.length > 0) {
        categorizedSub.push({
          type: type,
          name: categoryName,
          items: items
        });
      }
    });

    this.setData({
      catelist: {
        ...this.data.catelist,
        categorizedSub: categorizedSub
      }
    });
  },

  toPlaylist(e) {

  },

  

  onReady() {

  },


  onShow() {

  },


  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})